package net.tmaize.service;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import net.tmaize.dao.impl.LatestDaoImpl;
import net.tmaize.entity.News_latest;

public class ShowLatest extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");

		// 不缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", -1);
		try {
			LatestDaoImpl latestDaoImpl = new LatestDaoImpl();
			News_latest latest = latestDaoImpl.getLatest();
			request.setAttribute("latest", latest);
			RequestDispatcher requestDispatcher = this.getServletContext()
					.getRequestDispatcher("/WEB-INF/jsp/latest.jsp");
			requestDispatcher.forward(request, response);
		} catch (JSONException e) {
			// 解析json失败
			response.sendRedirect(request.getContextPath() + "/error.html");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
